<template>
	<!--pages/card/card.wxml-->
	<block>
		<view v-if="empty" class="empty">该礼品卡已被领取</view>
		<block v-else>
			<view class="typeCard">
				<view class="cardIImg">
					<image :src="gift.img_url"></image>
				</view>
				<view class="cardType">{{gift.title}}</view>
				<view class="cardCost">
					<view class="costList">
						<view class="costTitle">总面值</view>
						<view class="price">￥{{gift.money}}</view>
					</view>
					<view class="costList">
						<view class="costTitle">总张数</view>
						<view class="num">1张</view>
					</view>
				</view>
			</view>
			<view class="time-block">
				<view class="cardList">
					<view>有效时间</view>
					<view>{{gift.time}}</view>
				</view>
				<view class="cardList">
					<view class='use-know'>使用须知</view>
					<view class="use-note">
						<block v-for="(item, index) in gift.notes" :key="index">
							<view>{{item}}</view>
						</block>
					</view>
				</view>
			</view>
			<view class="time-block-bottom"></view>
			<!-- 领取人 -->
			<view v-if="openType==2" class="footerUse">
				<block v-if="gift.status!=1">
					<view class="useList" @tap="toReceive">
						<view class="useImg">
							<image src="/static/images/gift_self.png"></image>
						</view>
						<view>领取</view>
					</view>
				</block>
				<block v-else>
					<view class="useList">
						<view class="useImg">
							<image src="/static/images/gift_vip.png"></image>
						</view>
						<view>该礼品卡已被领取</view>
					</view>
				</block>
			</view>
			<!-- 转发人 -->
			<view v-else class="footerUse">
				<block v-if="gift.status==0">
					<view v-if="!gift.p_xcx_vid" class="useList share">
						<view class="useImg">
							<image src="/static/images/gift_share.png"></image>
						</view>
						<view>转赠</view>
						<button open-type="share">转赠</button>
					</view>
					<view class="useList" @tap="Recharge">
						<view class="useImg">
							<image src="/static/images/gift_vip.png"></image>
						</view>
						<view>充值到余额</view>
					</view>
				</block>
				<block v-else>
					<view v-if="!gift.recharge_vid&&gift.status_type==2" class="useList">
						<view class="useImg">
							<image src="/static/images/gift_share.png"></image>
						</view>
						<view>已转赠</view>
					</view>
					<view v-else-if="gift.recharge_vid&&gift.status_type==1" class="useList">
						<view class="useImg">
							<image src="/static/images/gift_vip.png"></image>
						</view>
						<view>已充值到余额</view>
					</view>
				</block>
			</view>
		</block>
	</block>
</template>

<script>
	// pages/card/card.js
	var app = getApp().globalData;

	export default {
		data() {
			return {
				xcx_vid: "",
				id: "",
				openType: "",
				gift: "",
				empty: 0
			};
		},

		components: {},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		async onLoad(options) {
			// #ifdef MP-ALIPAY
			if (JSON.stringify(options) == '{}') {
				options = my.getLaunchOptionsSync().query;
			}
			// #endif
			console.log(options);
			await this.$onLaunched;

			if (options && (options.store_id || options.storeid)) {
				this.$store.store_id = options.store_id ? options.store_id : options.storeid;
			}

			if (options.xcx_vid) {
				this.setData({
					xcx_vid: options.xcx_vid
				});
			}

			this.setData({
				id: options.id,
				openType: options.type && options.type == 2 ? 2 : 1
			});

			this.getDetail();
		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage(res) {
			var that = this;
			var path = '/pages/mine/gift_card/gift_card?id=' + that.id + '&type=2&xcx_vid=' + that.gift.xcx_vid +
				'&store_id=' + this.$store.store_id + '&share_uid=' + app.XcxData.userInfo.uid;
			console.log(path);
			return {
				title: that.gift.money + '元' + that.gift.title + '礼品卡',
				path: path,
				imageUrl: that.gift.img_url
			};
		},
		methods: {
			// 充值到余额
			Recharge: function() {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '充值到会员卡余额后,将无法在转赠给他人,确定充值吗？',
					showCancel: true,
					confirmColor: '#ff5c6d',
					success: function(res) {
						if (res.confirm) {
							getApp().globalData.sendRequest({
								url: '/GiftCard/giftSelfUse',
								type: "post",
								data: {
									id: that.id
								},
								success: function(res) {
									if (res.status == 1) {
										uni.showToast({
											title: res.msg,
											duration: 1500,
											mask: true,
											success: function() {
												setTimeout(function() {
													that.getDetail();
												}, 1000);
											}
										});
									} else if (res.status == 2) {
										uni.showModal({
											title: '提示',
											content: '暂未绑定会员卡？',
											showCancel: true,
											confirmText: '去绑定',
											confirmColor: '#ff5c6d',
											success: function(res) {
												if (res.confirm) {
													uni.navigateTo({
														url: '../card/card'
													});
												}
											}
										});
										return;
									} else {
										uni.showToast({
											title: res.msg,
											icon: 'none',
											duration: 2000,
											mask: true
										});
									}
								}
							});
						}
					}
				});
			},
			getDetail: function() {
				var that = this;
				getApp().globalData.sendRequest({
					url: '/GiftCard/getMyGiftDetail',
					type: "post",
					data: {
						id: that.id,
						type: that.openType,
						xcx_vid: that.xcx_vid
					},
					success: function(res) {
						if (res.status == 1) {
							that.setData({
								gift: res.data,
								id: res.data.id
							});
						} else {
							that.setData({
								empty: 1
							});
						}
					}
				});
			},
			//领取
			toReceive: function() {
				var that = this;
				getApp().globalData.verifyLogin('/pages/mine/gift_card/gift_card?id=' + that.id + '&type=2&xcx_vid=' +
					that.xcx_vid, () => {
						getApp().globalData.sendRequest({
							url: '/GiftCard/toReceive',
							type: "post",
							data: {
								id: that.id
							},
							success: function(res) {
								if (res.status == 1) {
									uni.showToast({
										title: res.msg,
										duration: 1500,
										mask: true,
										success: function() {
											setTimeout(function() {
												var gift = that.gift;
												gift.p_xcx_vid = 1;
												that.setData({
													xcx_vid: '',
													openType: 1,
													id: res.id,
													gift: gift
												});
												that.getDetail();
											}, 1000);
										}
									});
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none',
										duration: 1500,
										mask: true
									});
								}
							}
						});
					})
			}
		}
	};
</script>
<style>
	@import "./gift_card.css";
</style>
